home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / TouchMe 1.1.1.sit / touchMe 1.11 Folder / CW9 PP source / source / CTouchMeApp.cp next >
Text File  |  1996-08-08  |  13KB  |  523 lines

  1. // ==================================================
  2. //    CTouchMeApp.cp
  3. //    Copyright (C) 1996 Mizutori Tetsuya, July 4 1996, August 4, 1996.
  4. // ==================================================
  5. //    All documents are pretty-printed in Geneva 10-point font.
  6.  
  7. #include <TextUtils.h>
  8. //#include <Balloons.h>
  9.  
  10. #include <LGrowZone.h>
  11. #include <LDocApplication.h>
  12. #include <LWindow.h>
  13. #include <LMenuBar.h>
  14. #include <LMenu.h>
  15. #include <LString.h>
  16.  
  17. #include <PP_Messages.h>
  18. #include <PP_Resources.h>
  19. #include <PPobClasses.h>
  20.  
  21. #include <UDrawingState.h>
  22. #include <UMemoryMgr.h>
  23. #include <URegistrar.h>
  24. #include <UDesktop.h>
  25. #include <UWindows.h>
  26.  
  27. #include "touchMeConstants.h"
  28. #include "touchMeRegistry.h"
  29. #include "CTouchMeAppleEvents.h"
  30. #include "CTouchMeApp.h"
  31. #include "CTouchMePref.h"
  32. #include "CRadioButton.h"
  33. #include "CDateEditField.h"
  34. #include "CTouchMeDialog.h"
  35. #include "LHelpMenu.h"
  36. #include "UFileInfo.h"
  37. #include "UErrorMessage.h"
  38.  
  39.  
  40. // Patch for the colored alert dialog. Thanks to Rokkaku Fumio.
  41. #define PATCH_ACTB TRUE
  42.  
  43. #ifdef PATCH_ACTB
  44. static pascal Boolean    MyAlertPatch( DialogRef dialog, EventRecord *event,  short *item );
  45. ModalFilterUPP        AlertPatchUPP;
  46. #endif // PATCH_ACTB
  47.  
  48.  
  49. CTouchMePref *        gPref        = nil;
  50. CTouchMeDialog *    gDialog    = nil;
  51. LHelpMenu *        gHelpMenu = nil;
  52.  
  53.  
  54. // --------------------------------------------------
  55. //        ・ Main Program
  56. // --------------------------------------------------
  57.  
  58. void main( void )
  59. {
  60.     SetDebugThrow_( debugAction_Alert );
  61.     SetDebugSignal_( debugAction_Alert );
  62.  
  63.     InitializeHeap( 3 );
  64.     UQDGlobals::InitializeToolbox( &qd );
  65.  
  66.     new LGrowZone( 20000 );
  67.  
  68.     // Create a preferences class object corresponding to "touchMe Prefs" file.
  69.     Str255    theFilename;
  70.     ::GetIndString( theFilename, rSTRx_TouchMe, rSTRx_TouchMe_PrefFile );
  71.     gPref = new CTouchMePref( theFilename );
  72.  
  73.     // Initialize the preferences reading from preferences file, or by default value.
  74.     gPref->LoadPrefData();
  75.  
  76.     CTouchMeApp    theApp;
  77.     theApp.Run();
  78. }
  79.  
  80.  
  81. // --------------------------------------------------
  82. //        ・ CTouchMeApp 
  83. // --------------------------------------------------
  84. //    Constructor
  85.  
  86. CTouchMeApp::CTouchMeApp()
  87. {
  88.     // Add Help Menu item. My 'LHelpMenu' is a subclass of the standard 'LMenu'.
  89.     Str255        theMenuItemString;
  90.     ::GetIndString( theMenuItemString, rSTRx_TouchMe, rSTRx_TouchMe_HelpMenu );
  91.     gHelpMenu = (LHelpMenu *) new LHelpMenu();
  92.     gHelpMenu->InsertCommand( theMenuItemString, cmd_HelpMenu, 255 /*at the end of menu*/ );
  93.     gHelpMenu->InstallMenu( (LMenuBar *) LMenuBar::GetCurrentMenuBar() );
  94.  
  95.     // Register functions to create core PowerPlant classes.
  96. //    RegisterAllPPClasses();
  97.     RegisterPainClasses();
  98.  
  99.     // Setup my program process status.
  100.     mOpenApplication = false;
  101.     mOpenDocument = false;
  102.     mKeyModifier = false;
  103.     mCountDoc = 0;
  104.  
  105. #ifdef PATCH_ACTB
  106.     AlertPatchUPP = NewModalFilterProc( MyAlertPatch );
  107. #endif // PATCH_ACTB
  108. }
  109.  
  110.  
  111. // --------------------------------------------------
  112. //        ・ ~CTouchMeApp
  113. // --------------------------------------------------
  114. //    Destructor
  115.  
  116. CTouchMeApp::~CTouchMeApp()
  117. {
  118. #ifdef PATCH_ACTB
  119.  
  120. //    if ( AlertPatchUPP != nil ) {
  121. //        DisposeRoutineDescriptor( AlertPatchUPP );
  122. //    }
  123. //    AlertPatchUPP = nil;
  124.  
  125. #endif // PATCH_ACTB
  126. }
  127.  
  128.  
  129. // --------------------------------------------------
  130. //        ・ StartUp
  131. // --------------------------------------------------
  132.  
  133. void
  134. CTouchMeApp::StartUp()
  135. {
  136.     // Setup my program process status.
  137.     mOpenApplication = true;
  138.  
  139.     // Create the preferences dialog.
  140.     gDialog = (CTouchMeDialog *)
  141.         LWindow::CreateWindow( rPPob_TouchMeDialog, this );
  142.     Assert_( gDialog != nil );
  143.  
  144.     // Move the dialog position before 'Show()', not to emerge a ghost!
  145.     gDialog->MoveDialog( *gPref );
  146.  
  147.     // Show the dialog.
  148.     // Setup dialog status, such as of RadioButtons, after 'Show()'.
  149.     gDialog->Show();
  150.     gDialog->SetupDialog( *gPref );
  151. }
  152.  
  153.  
  154. // --------------------------------------------------
  155. //        ・ ObeyCommand
  156. // --------------------------------------------------
  157. //    Respond to commands
  158.  
  159. Boolean
  160. CTouchMeApp::ObeyCommand(
  161.     CommandT    inCommand,
  162.     void        *ioParam)
  163. {
  164.     Boolean    cmdHandled = true;
  165.  
  166.     switch ( inCommand ) {
  167.         case msg_TmeD_ButtonOK:
  168.         case msg_TmeD_ButtonCencel:
  169.         {
  170.             delete gPref;
  171.             delete gDialog;
  172.  
  173.             SendAEQuit();
  174.         }
  175.         break;
  176.  
  177.         case cmd_Save:
  178.         case cmd_SavePrefs:
  179.         {
  180.             // Save all the settings to the preferences file.
  181.             gDialog->InspectDialog( *gPref );
  182.             gPref->SavePrefData();
  183.         }
  184.         break;
  185.  
  186.         case cmd_SavePrefsWFrame:
  187.         {
  188.             // Save the window position only, but not all the other settings.
  189.             Rect        theRect;
  190.             gDialog->GetGlobalFrameRect( theRect );
  191.  
  192.             // Revert the settings to read from preferences file, and set the Window position.
  193.             gPref->LoadPrefData();
  194.             gPref->SetWindowRect( theRect );
  195.             gPref->SavePrefData();
  196.         }
  197.         break;
  198.  
  199.         case cmd_Touch:
  200.         {
  201.             FSSpec    * theFSSpec = (FSSpec *) ioParam;
  202.             SendAEOpenDoc( *theFSSpec );
  203.             //OpenDocument( theFSSpec );
  204.         }
  205.         break;
  206.  
  207.         case cmd_Close:
  208.         {
  209.             LWindow *    theHelpWindow = FindHelpWindow(rPPob_HelpWindow);
  210.  
  211.             if ( theHelpWindow != nil )  delete theHelpWindow;
  212.         }
  213.         break;
  214.  
  215.         case msg_Open_HelpWindow:
  216.         case cmd_HelpMenu:
  217.         {
  218.             // Open the help window. What I should do is just to call 'CreateWindow()'.
  219.             // Any other performance, including a scroll, a resize, a move and others,
  220.             // is the duty of 'LWindow' class object. Thanks to our powerful PowerPlant!
  221.             LWindow *    theHelpWindow = FindHelpWindow(rPPob_HelpWindow);
  222.  
  223.             if ( theHelpWindow != nil ) {
  224.                 theHelpWindow->Select();
  225.             } else {
  226.                 LWindow::CreateWindow(rPPob_HelpWindow, this);
  227.             }
  228.         }
  229.         break;
  230.  
  231.         default:
  232.         {
  233.             cmdHandled = LDocApplication::ObeyCommand( inCommand, ioParam );
  234.         }
  235.         break;
  236.     }
  237.  
  238.     return cmdHandled;
  239. }
  240.  
  241.  
  242. // --------------------------------------------------
  243. //        ・ FindHelpWindow
  244. // --------------------------------------------------
  245.  
  246. #ifdef COMMENT
  247.  
  248. // Macintosh Toolbox version (but this time, we do not used it)
  249. LWindow * 
  250. CTouchMeApp::FindHelpWindow(
  251.     const ResIDT    inWindowID )
  252. {
  253.     WindowPtr        theMacWindowP;
  254.     LWindow*        theWindowObj;
  255.  
  256.     theMacWindowP = ::FrontWindow();
  257.  
  258.     while ( theMacWindowP != nil ) {
  259.         theWindowObj = LWindow::FetchWindowObject( theMacWindowP );
  260.         if ( theWindowObj != nil && theWindowObj->GetUserCon() == inWindowID ) {
  261.             return theWindowObj;
  262.         }
  263.         theMacWindowP = (WindowPtr) ((WindowPeek) theMacWindowP)->nextWindow;
  264.     }
  265.  
  266.     return  (LWindow*) nil;
  267. }
  268.  
  269. #else // COMMENT
  270.  
  271. // Codewarrior PowerPlant version using utility class 'UWindows'
  272. LWindow * 
  273. CTouchMeApp::FindHelpWindow(
  274.     const ResIDT    inWindowID )
  275. {
  276.     short            index = 0;
  277.     WindowPtr        theMacWindowP;
  278.     LWindow*        theWindowObj;
  279.  
  280.     while ( (theMacWindowP = UWindows::FindNthWindow(++index)) != nil ) {
  281.         theWindowObj = LWindow::FetchWindowObject( theMacWindowP );
  282.         if ( theWindowObj != nil && theWindowObj->GetUserCon() == inWindowID ) {
  283.             return theWindowObj;
  284.         }
  285.     }
  286.  
  287.     return  (LWindow*) nil;
  288. }
  289.  
  290. #endif // COMMENT
  291.  
  292.  
  293. // --------------------------------------------------
  294. //        ・ FindCommandStatus
  295. // --------------------------------------------------
  296.  
  297. void
  298. CTouchMeApp::FindCommandStatus(
  299.     CommandT        inCommand,
  300.     Boolean        &outEnabled,
  301.     Boolean        &outUsesMark,
  302.     Char16        &outMark,
  303.     Str255        outName )
  304. {
  305.     // do nothing else, here.
  306.     switch (inCommand) {
  307.         case cmd_About:
  308.         case cmd_Open:
  309.         case cmd_Close:
  310.         case cmd_Quit:
  311.             outEnabled = true;
  312.             break;
  313.         case cmd_New:
  314.         case cmd_PageSetup:
  315.             outEnabled = false;
  316.             break;
  317.         default:
  318.             LDocApplication::FindCommandStatus( inCommand,
  319.                 outEnabled, outUsesMark, outMark, outName );
  320.             break;
  321.     }
  322. }
  323.  
  324.  
  325. // --------------------------------------------------
  326. //        ・ UseIdleTime
  327. // --------------------------------------------------
  328.  
  329. void
  330. CTouchMeApp::UseIdleTime(
  331.     const EventRecord &    inMacEvent )
  332. {
  333.     LDocApplication::UseIdleTime( inMacEvent );
  334.  
  335.     // Check the modifier key while opening the documents.
  336.     mKeyModifier = ( (inMacEvent.modifiers & optionKey) != 0 );
  337.     if ( gDialog != nil )  gDialog->Indicator( mKeyModifier );
  338.  
  339.     // Count the items in the document list thrown by an OpenDocument event.
  340.     mCountDoc = 0;
  341.  
  342.     // To quit immediately after the drag&drop operation finished.
  343.     // Do not quit when you have opened the application by a double-clicking.
  344.     if ( ! mOpenApplication  && mOpenDocument )  SendAEQuit();
  345. }
  346.  
  347.  
  348. // --------------------------------------------------
  349. //        ・ UpdateMenus
  350. // --------------------------------------------------
  351.  
  352. void
  353. CTouchMeApp::UpdateMenus()
  354. {
  355.     LDocApplication::UpdateMenus();
  356.  
  357.     gHelpMenu->EnableItem();
  358. }
  359.  
  360.  
  361. // --------------------------------------------------
  362. //        ・ ShowAboutBox
  363. // --------------------------------------------------
  364.  
  365. void
  366. CTouchMeApp::ShowAboutBox()
  367. {
  368. #ifdef PATCH_ACTB
  369.  
  370.     UDesktop::Deactivate();
  371.     ::Alert( ALRT_About, AlertPatchUPP );
  372.     UDesktop::Activate();
  373.  
  374. #else // PATCH_ACTB
  375.  
  376.     LDocApplication::ShowAboutBox();
  377.  
  378. #endif // PATCH_ACTB
  379. }
  380.  
  381.  
  382. // --------------------------------------------------
  383. //        ・ OpenDocument
  384. // --------------------------------------------------
  385. // This procedure is the major part of touchMe program.
  386. // If option-key is pressed, it works as a 'fetch' command.
  387. // Otherwise, it works as a 'touch' command regularly.
  388.  
  389. void
  390. CTouchMeApp::OpenDocument(
  391.     FSSpec    *inMacFSSpec )
  392. {
  393.     // Setup my program process status.
  394.     mOpenDocument = true;
  395.  
  396.     // Check the modifier key whether option-key is pressed or not.
  397.     // If option-key is pressed, it works as a 'fetch' command and exits immediately.
  398.     if ( mKeyModifier ) {
  399.         if ( gDialog != nil ) gDialog->BroadcastMessage( msg_TmeD_DroppedFile, inMacFSSpec );
  400.         return;
  401.     }
  402.  
  403.     // Count up the number at every execution for each file in the document list.
  404.     mCountDoc ++;
  405.  
  406.     static unsigned long    theCrDateTime = 0, theMdDateTime = 0;
  407.  
  408.     // Do special for the first item.
  409.     if ( mCountDoc == 1 )  {
  410.         // If dialog window is open, then read the settings from the dialog.
  411.         if ( gDialog != nil ) gDialog->InspectDialog( *gPref );
  412.  
  413.         // Fetch the date time stamp from the first dropped file.
  414.         UFileInfo::GetFSSpecDateTime( *inMacFSSpec, theCrDateTime, theMdDateTime );
  415.  
  416.         unsigned long    theDateTimeSecs;
  417.                     ::GetDateTime( &theDateTimeSecs );
  418.  
  419.         // Creation date:
  420.         // Setup the date time stamp according to the flag of settings.
  421.         if ( gPref->GetFlag( touchType_CreationDate, touchFlag_Current ) ) {
  422.             theCrDateTime = theDateTimeSecs;
  423.         } else if (  gPref->GetFlag( touchType_CreationDate, touchFlag_Direct ) ) {
  424.             theCrDateTime = gPref->GetDateTime( touchType_CreationDate );
  425.         } else if ( gPref->GetFlag( touchType_CreationDate, touchFlag_Second ) ) {
  426.             gPref->SetDateTime( touchType_CreationDate, theCrDateTime );
  427.             if ( gDialog != nil ) gDialog->SetupDialog( *gPref );
  428.         }
  429.  
  430.         // Modification date:
  431.         // Setup the date time stamp according to the flag of settings.
  432.         if ( gPref->GetFlag( touchType_ModificationDate, touchFlag_Current ) ) {
  433.             theMdDateTime = theDateTimeSecs;
  434.         } else if (  gPref->GetFlag( touchType_ModificationDate, touchFlag_Direct ) ) {
  435.             theMdDateTime = gPref->GetDateTime( touchType_ModificationDate );
  436.         } else if ( gPref->GetFlag( touchType_ModificationDate, touchFlag_Second ) ) {
  437.             gPref->SetDateTime( touchType_ModificationDate, theMdDateTime );
  438.             if ( gDialog != nil ) gDialog->SetupDialog( *gPref );
  439.         }
  440.     }
  441.  
  442.     // If the{Cr,Md}DateTime has a zero value, then its date time of FSSpec is not changed.
  443.     if ( ! gPref->GetEnabled( touchType_CreationDate ) )  theCrDateTime = 0;
  444.     if ( ! gPref->GetEnabled( touchType_ModificationDate ) )  theMdDateTime = 0;
  445.     UFileInfo::SetFSSpecDateTime( *inMacFSSpec, theCrDateTime, theMdDateTime );
  446. }
  447.  
  448.  
  449. // --------------------------------------------------
  450. //        ・ ChooseDocument
  451. // --------------------------------------------------
  452.  
  453. void
  454. CTouchMeApp::ChooseDocument()
  455. {
  456.     // Deactivate the desktop.
  457.     UDesktop::Deactivate();
  458.  
  459.     // Browse for a document.
  460.     SFTypeList            theTypeList;
  461.     StandardFileReply    theReply;
  462.     ::StandardGetFile( nil, -1, theTypeList, &theReply );
  463.  
  464.     // Activate the desktop.
  465.     UDesktop::Activate();
  466.  
  467.     // Send an AppleEvent to open the file.    
  468.     if ( theReply.sfGood ) SendAEOpenDoc( theReply.sfFile );
  469. }
  470.  
  471.  
  472. #ifdef PATCH_ACTB
  473. // --------------------------------------------------
  474. //        ・ MyAlertPatch
  475. // --------------------------------------------------
  476. // Patch for the colored alert dialog. Thanks to Rokkaku Fumio.
  477.  
  478. #include  <UKeyFilters.h>
  479.  
  480. static pascal Boolean
  481. MyAlertPatch(
  482.     DialogRef        theDialog,
  483.     EventRecord *    theEvent,
  484.     short *        itemHit )
  485. {
  486.     Boolean        eventOccurred = false;
  487.  
  488.     switch ( theEvent->what ) {
  489.         case keyDown:
  490.         {
  491.             short    charCode = (theEvent->message & charCodeMask);
  492. //            if ( (charCode ==kEnterKey ) || (charCode ==kReturnKey) ) {
  493.             if ( UKeyFilters::IsActionKey( charCode ) ) {
  494.                 ControlRef    aButton;
  495.                 Rect        aRect;
  496.                 short        aType;
  497.                 long        finalTick;
  498.  
  499.                 ::GetDialogItem( theDialog, ok, &aType, (Handle *)&aButton, &aRect );
  500.                 ::HiliteControl( aButton, kControlButtonPart );
  501.                 ::Delay( 8, &finalTick );
  502.                 ::HiliteControl( aButton, kControlNoPart );
  503.                 *itemHit = kStdOkItemIndex;
  504.                 eventOccurred = true;
  505.             }
  506.         }
  507.         break;
  508.  
  509.         case updateEvt:
  510.         {
  511.             WindowRef        theWindow = GetDialogWindow( theDialog );
  512.             if ( (WindowRef)theEvent->message == theWindow ) {
  513.                 ::SelectWindow( theWindow );
  514.             }
  515.         }
  516.         break;
  517.     }
  518.     return eventOccurred;
  519. }
  520. #endif // PATCH_ACTB
  521.  
  522. // end of program
  523.